package com.pccc.batch.config.remotepartition.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.integration.partition.StepExecutionRequest;
import org.springframework.batch.integration.partition.StepExecutionRequestHandler;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;

@Service
public class StepExecutionActivator {

	private StepExecutionRequestHandler stepExecutionRequestHandler;
	
	public StepExecutionActivator(StepExecutionRequestHandler stepExecutionRequestHandler) {
		this.stepExecutionRequestHandler = stepExecutionRequestHandler;
	}
	
	@ServiceActivator(inputChannel = "inputRequest", outputChannel = "replyMaster",async="true")
	public Message<StepExecution> handleMessage(Message<?> message,@Header(value = "correlationid") String correlationId) throws MessagingException {
		StepExecutionRequest stepExec = (StepExecutionRequest)message.getPayload();
		StepExecution execution = stepExecutionRequestHandler.handle(stepExec);
		Map<String,Object> headermap = new HashMap<String, Object>();
		headermap.put("correlationId", correlationId);
		MessageHeaders headers = new MessageHeaders(headermap);
		Message<StepExecution> response = new GenericMessage<StepExecution>(execution, headers);
		return response;
	}

	
}
